package leetcode.code0868;

public class Solution {

	public int binaryGap(int n) {
		int ans = -1;
		while (n > 0) {
			while (n > 0 && (n & 1) != 1) {
				n >>= 1;
			}
			n >>= 1;
			int cur = 0;
			if (n > 0) {
				while (n > 0 && (n & 1) != 1) {
					n >>= 1;
					cur++;
				}
				ans = Math.max(ans, cur);
			}
		}
		return ans + 1;
	}

	public static void main(String[] args) {
		for (int i = 0; i < 20; i++) {
			System.out.println(i + " " + ((i + 1) >> 1)+ " " + ((i) >> 1));
		}

	}

}
